
class WikiController < ApplicationController
  before_action :set_wiki, only: [:show]
  layout "product"

  def index
    @wiki_categories = WikiCategory.all
    basic_query = Wiki.where('1=1')
    basic_query = basic_query.where(wiki_category_id: params[:wiki_category_id]) if params[:wiki_category_id]
    @wikis = basic_query.paginate(page: params[:page] || 1, per_page: params[:per_page] || 10).order("id desc")
    respond_to do |format|
      format.html
      format.json { render json:  {status: 'ok', wikis: @wikis} }
    end
  end

  def show
    @last = Wiki.where('id < ?', @wiki.id).first
    @next = Wiki.where('id > ?', @wiki.id).first
    @related = Wiki.where("wiki_category_id = ? and id != ?", @wiki.wiki_category_id, @wiki.id).order('publish_time DESC').limit(5)
    @sales_promotions = SalesPromotion.actived.limit(2)
    # binding.pry
  end

  private

  def set_wiki
    @wiki = Wiki.find(params[:id])
  end

end
